// 目标元素
const targetElement = document.querySelector('body');

const iframeMap = new WeakMap()
// 创建一个MutationObserver实例
const observer = new MutationObserver((mutations) => {
  // console.log(mutations)
  console.count('mutations')
  let iframe = document.querySelector('iframe')
  if (iframe && !iframeMap.has(iframe)) {
    iframeMap.set(iframe, iframe)
    let win = iframe.contentWindow
    let originSend = win.XMLHttpRequest.prototype.send;
    win.XMLHttpRequest.prototype.send = function() {
      this.setRequestHeader("Authorization", 'my-token')
      // alert('拦载iframe内的xhr请求！')
      originSend.apply(this, arguments);
    }
  }
  /* mutations.forEach((mutation) => {
    if (mutation.attributeName === 'src') {
      console.log(mutation)
    }

    mutation.addedNodes.forEach((addedNode) => {
      console.log(`添加了子元素：${addedNode.nodeName}`);
      // 执行相应的处理逻辑
    });

    // if (mutation.type === 'childList') {
    //   mutation.addedNodes.forEach((addedNode) => {
    //     console.log(`添加了子元素：${addedNode.nodeName}`);
    //     // 执行相应的处理逻辑
    //   });

    //   mutation.removedNodes.forEach((removedNode) => {
    //     console.log(`移除了子元素：${removedNode.nodeName}`);
    //     // 执行相应的处理逻辑
    //   });
    // }
  }); */
});

// 配置观察器
const config = {
  childList: true,
  subtree: true,
  // attributes: true,
  // attributeFilter: ['src']
};

// 启动观察器
observer.observe(targetElement, config);